export function textWrap(ctx: CanvasRenderingContext2D, text: string, width?: number): string[] {
  const result: string[] = [];
  if (width === undefined) {
    return text.split("\n");
  }

  let flg = 0;
  let tempStr = "";
  while (flg < text.length) {
    const str = text[flg];
    if (str === "\n") {
      result.push(tempStr);
      tempStr = "";
    } else {
      const tempStrWidth = ctx.measureText(tempStr + str).width;
      if (tempStrWidth > width) {
        result.push(tempStr);
        tempStr = str;
      } else {
        tempStr += str;
      }
    }
    flg++;
    if (flg === text.length) {
      result.push(tempStr);
    }
  }
  return result;
}
